package Offer.offer;

import java.util.HashSet;
import java.util.Set;

import scala.Int;

/**
 * 字符串的排列
 *
 * @author leiwenkang
 * @date 2020/4/7
 */
public class Test28字符串的排列 {
    public static void main(String[] args) {
        char[] ch = {'a', 'b', 'b'};
        permutation(ch);
    }

    public static void permutation(char[] ch) {
        if (ch == null || ch.length == 0) {
            return;
        }
        permutation(ch, 0);
    }

    public static void permutation(char[] ch, int start) {
        if (start == ch.length - 1) {
            System.out.print(ch);
            System.out.print(" ");
        }

        Set<Character> set = new HashSet<>();
        for (int i = start; i <= ch.length - 1; i++) {
            if (i == start || !set.contains(ch[i])) {
                set.add(ch[i]);
                swap(ch, i, start);
                permutation(ch, start + 1);
                swap(ch, i, start);
            }
        }

    }

    private static void swap(char[] array, int i, int j) {
        char temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
